package com.gxa.controller.commlife;

import com.gxa.common.utils.Result;
import com.gxa.entity.commlife.MerchantType;
import com.gxa.service.commlife.MerchantTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:18:00
 */
@Api(tags = "商家类型")
@RestController
@RequestMapping("/merchant-type")

public class MerchantTypeController {

    @Autowired
    private MerchantTypeService merchantTypeService;

//    List<MerchantType> merchantTypeList;
//    Date date = new Date("2020/10/23");
//    public MerchantTypeController(){
//        this.init();
//    }
//    void init(){
//        merchantTypeList = new ArrayList<>();
//        MerchantType merchantType = new MerchantType(1,"餐饮服务",date,0);
//        merchantTypeList.add(merchantType);
//        MerchantType merchantType1 = new MerchantType(2,"餐饮服务",date,0);
//        merchantTypeList.add(merchantType1);
//    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询商家类型")
    @GetMapping("list/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){
        try{
            Map<String, Object> map = this.merchantTypeService.queryList("",pageNum, pageSize);
            Result<Map> result = Result.success(map);
            return result;
        }catch(Exception e){
            return Result.failed("查询失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "搜索商家类型")
    @PostMapping("search")
    public Result search(String typeName,Integer pageNum,Integer pageSize){
        try{
            System.out.println(typeName);
            Map<String, Object> map = this.merchantTypeService.queryList(typeName, pageNum, pageSize);
            Result<Map> result = Result.success(map);
            return result;
        }catch (Exception e){
            return  Result.failed("搜索失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "增加商家类型")
    @PostMapping("add")
    public Result add(@RequestBody MerchantType merchantType){
        try{
            long l = System.currentTimeMillis();
            Date date = new Date(l);
            merchantType.setCreateTime(date);
            this.merchantTypeService.addMerchantTypeMapper(merchantType);
            Result<Object> result = Result.success("添加成功");
            result.setMsg("添加成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("添加失败");
        }

    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询当前商家类型")
    @GetMapping("preEdit/{id}")
    public Result preEdit(@PathVariable("id") Integer id){
      try {
          MerchantType merchantType = this.merchantTypeService.queryById(id);
          Result<MerchantType> result = Result.success(merchantType);
          return result;
      }catch (Exception e){
          e.printStackTrace();
          return Result.failed("查询失败");
      }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "修改商家类型")
    @PutMapping("edit")
    public Result edit(@RequestBody MerchantType merchantType){
        try{
            this.merchantTypeService.updateMerchantType(merchantType);
            Result<Object> result = Result.success("修改成功");
            result.setMsg("修改成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("修改失败");
        }

    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "删除商家类型")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
       try{
           this.merchantTypeService.deleteMerchantType(id);
           Result<Object> result = Result.success("删除成功");
           result.setMsg("删除成功");
           return result;
       }catch (Exception e){
           return Result.failed("删除失败");
       }

    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询商家类型(不分页)")
    @GetMapping("list")
    public Result list1(){
        try{
            List<MerchantType> merchantTypeList = this.merchantTypeService.queryList1();
            Result<List<MerchantType>> result = Result.success(merchantTypeList);
            return result;
        }catch(Exception e){
            return Result.failed("查询失败");
        }
    }


}
